package com.xxx.ware.ware_manage_sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.ware.ware_manage_sys.bean.dto.GoodsCateQueryDto;
import com.xxx.ware.ware_manage_sys.bean.dto.GoodsCateUpdateSaveDto;
import com.xxx.ware.ware_manage_sys.bean.dto.WebResultDto;
import com.xxx.ware.ware_manage_sys.bean.entity.goods_cate.WmsGoodsCategory;
import com.xxx.ware.ware_manage_sys.bean.vo.GoodsCateVo;
import com.xxx.ware.ware_manage_sys.mapper.WmsGoodsCategoryMapper;
import com.xxx.ware.ware_manage_sys.service.IWmsGoodsCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WmsGoodsCategoryServiceImpl extends ServiceImpl<WmsGoodsCategoryMapper, WmsGoodsCategory> implements IWmsGoodsCategoryService {
    @Override
    public WebResultDto<GoodsCateVo> getGoodsCateList(GoodsCateQueryDto goodsCateQueryDto) {
        Integer pageNo = goodsCateQueryDto.getPageNo();
        Integer pageSize = goodsCateQueryDto.getPageSize();
        String goodsCateName = goodsCateQueryDto.getGoodsCateName();
        LambdaQueryWrapper<WmsGoodsCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WmsGoodsCategory::getStatus, 1);
        queryWrapper.orderByDesc(WmsGoodsCategory::getCreateTime);
        if (pageNo != null && pageSize != null) {
            log.info("商品分类，分页查询，入参：{}", goodsCateQueryDto);
            if (StringUtils.isNotBlank(goodsCateName)) {
                queryWrapper.likeRight(WmsGoodsCategory::getGoodsCateName, goodsCateName);
            }
            IPage<WmsGoodsCategory> page = new Page<>(pageNo, pageSize);
            IPage<WmsGoodsCategory> resultPage = this.getBaseMapper().selectPage(page, queryWrapper);
            if (resultPage == null ||
                    resultPage.getRecords() == null ||
                    resultPage.getRecords().isEmpty()) {
                return WebResultDto.getDataList(pageNo, pageSize, 0L, null);
            }

            List<GoodsCateVo> resultList = resultPage.getRecords().stream().map(e ->
                    {
                        GoodsCateVo goodsCateVo = new GoodsCateVo();
                        goodsCateVo.setId(e.getId());
                        goodsCateVo.setGoodsCateName(e.getGoodsCateName());
                        goodsCateVo.setStatus(e.getStatus().equals(1) ? "生效" : "失效");
                        goodsCateVo.setCreateTime(DateUtil.format(e.getCreateTime(), "yyyy-MM-dd HH:mm:ss"));
                        goodsCateVo.setUpdateTime(DateUtil.format(e.getUpdateTime(), "yyyy-MM-dd HH:mm:ss"));
                        goodsCateVo.setRemark(e.getRemark());
                        return goodsCateVo;
                    }
            ).collect(Collectors.toList());

            return WebResultDto.getDataList(pageNo, pageSize, resultPage.getTotal(), resultList);
        } else {//非分页查询情况
            log.info("商品分类，非分页查询，入参：{}", goodsCateQueryDto);
            queryWrapper.select(WmsGoodsCategory::getId, WmsGoodsCategory::getGoodsCateName);
            List<WmsGoodsCategory> wmsGoodsCategories = this.getBaseMapper().selectList(queryWrapper);
            if (CollectionUtil.isEmpty(wmsGoodsCategories)) {
                return WebResultDto.getDataList(0, 0, 0L, new ArrayList<>());
            }
            List<GoodsCateVo> resultList = wmsGoodsCategories.stream().map(e ->
                {
                    GoodsCateVo goodsCateVo = new GoodsCateVo();
                    goodsCateVo.setId(e.getId());
                    goodsCateVo.setGoodsCateName(e.getGoodsCateName());
                    return goodsCateVo;
                }
            ).collect(Collectors.toList());

            return WebResultDto.getDataList(0, 0, 0L, resultList);
        }
    }

    @Override
    public Boolean saveGoodsCate(GoodsCateUpdateSaveDto goodsCateUpdateSaveDto) throws Exception {
        String goodsCateName = goodsCateUpdateSaveDto.getGoodsCateName();
        String remark = goodsCateUpdateSaveDto.getRemark();
        WmsGoodsCategory soleGoodsCate = getGoodsCateBySoleKey(goodsCateName);
        if (soleGoodsCate != null) {
            throw new Exception("当前物品种类已存在，对应种类名称：" + goodsCateName);
        }
        WmsGoodsCategory wmsGoodsCategory = new WmsGoodsCategory();
        wmsGoodsCategory.setGoodsCateName(goodsCateName);
        wmsGoodsCategory.setRemark(remark);
        return save(wmsGoodsCategory);
    }

    @Override
    public Boolean updateGoodsCate(GoodsCateUpdateSaveDto goodsCateUpdateSaveDto) {
        WmsGoodsCategory wmsGoodsCategory = new WmsGoodsCategory();
        wmsGoodsCategory.setGoodsCateName(goodsCateUpdateSaveDto.getGoodsCateName());
        wmsGoodsCategory.setRemark(goodsCateUpdateSaveDto.getRemark());
        LambdaUpdateWrapper<WmsGoodsCategory> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(WmsGoodsCategory::getId, goodsCateUpdateSaveDto.getId());
        return update(wmsGoodsCategory, updateWrapper);
    }

    @Override
    public Boolean removeGoodsCate(Long id) {
        LambdaUpdateWrapper<WmsGoodsCategory> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(WmsGoodsCategory::getId, id);
        updateWrapper.set(WmsGoodsCategory::getStatus, 2);
        return update(updateWrapper);
    }

    @Override
    public WmsGoodsCategory getGoodsCateBySoleKey(String goodsCateName) {
        LambdaQueryWrapper<WmsGoodsCategory> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(WmsGoodsCategory::getGoodsCateName, goodsCateName);
        return this.getBaseMapper().selectOne(queryWrapper);
    }
}
